﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using System.Web;
using System.Reflection;
using NHibernate.Event;
using NHibernate.Event.Default;

namespace LibYanu.DAO
{
    public static class ConfigurationHelper
    {
        public static void ConfigureUsing(this Configuration cfg, string fileName)
        {
            string path = null;
            if (HttpRuntime.AppDomainId != null)
                path = HttpRuntime.AppDomainAppPath;
            else
            {
                var asm = System.Reflection.Assembly.GetExecutingAssembly();
                int pos = asm.Location.LastIndexOf('\\');
                int exePos = asm.Location.IndexOf(".exe");
                int dllPos = asm.Location.IndexOf(".dll");
                if (dllPos > 0 ||
                    exePos > 0)
                {
                    path = asm.Location.Substring(0, pos + 1);
                }
            }
            if (path != null)
                path += fileName;
            else
                path = System.IO.Path.GetFullPath(fileName);
            cfg.Configure(path);
        }

        public static void SetListeners(this Configuration cfg, Assembly asm)
        {
            foreach (var type in asm.GetTypes())
            {
                object listener = null;
                if (type.IsSubclassOf(typeof(DefaultSaveEventListener)))
                {
                    if (listener == null)
                        listener = Activator.CreateInstance(type);
                    cfg.SetListener(ListenerType.Save, listener);
                    cfg.SetListener(ListenerType.SaveUpdate, listener);
                }
                foreach (var iftype in type.GetInterfaces())
                {
                    if (iftype == typeof(IPreInsertEventListener))
                    {
                        if (listener == null)
                            listener = Activator.CreateInstance(type);
                        cfg.SetListener(ListenerType.PreInsert, listener);
                    }
                    if (iftype == typeof(IPreUpdateEventListener))
                    {
                        if (listener == null)
                            listener = Activator.CreateInstance(type);
                        cfg.SetListener(ListenerType.PreUpdate, listener);
                    }
                    if (iftype == typeof(IPreLoadEventListener))
                    {
                        if (listener == null)
                            listener = Activator.CreateInstance(type);

                        cfg.SetListener(ListenerType.PreLoad, listener);
                    }
                    if (iftype == typeof(IPreDeleteEventListener))
                    {
                        if (listener == null)
                            listener = Activator.CreateInstance(type);
                        cfg.SetListener(ListenerType.PreDelete, listener);
                    }
                    if (iftype == typeof(IPostInsertEventListener))
                    {
                        if (listener == null)
                            listener = Activator.CreateInstance(type);
                        //cfg.SetListener(ListenerType.PostInsert, listener);
                        cfg.SetListener(ListenerType.PostCommitInsert, listener);
                    }
                    if (iftype == typeof(IPostDeleteEventListener))
                    {
                        if (listener == null)
                            listener = Activator.CreateInstance(type);
                        cfg.SetListener(ListenerType.PostDelete, listener);
                        //cfg.SetListener(ListenerType.PostCommitInsert, listener);
                    }
                    if (iftype == typeof(IPostUpdateEventListener))
                    {
                        if (listener == null)
                            listener = Activator.CreateInstance(type);
                        cfg.SetListener(ListenerType.PostUpdate, listener);
                    }
                    if (iftype == typeof(IPostLoadEventListener))
                    {
                        if (listener == null)
                            listener = Activator.CreateInstance(type);
                        cfg.SetListener(ListenerType.PostLoad, listener);
                    }
                }
            }
        }
    }
}
